package com.example.testdb1.controller;

import com.example.testdb1.entity.Student;
import com.example.testdb1.entity.dto.PageDto;
import com.example.testdb1.entity.dto.StudentDto;
import com.example.testdb1.entity.dto.StudentLoginDto;
import com.example.testdb1.entity.result.R;
import com.example.testdb1.entity.vo.StudentPageVO;
import com.example.testdb1.mapper.StuMapper;
import com.example.testdb1.service.StuService;
import com.example.testdb1.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/stu")
@Api(tags = "后台端-学生信息")
@Slf4j
public class StuController {

    @Autowired
    private StuService stuService;
    @Autowired
    private StuMapper stuMapper;


    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@RequestBody StudentLoginDto studentLoginDto) {
        Student student = stuService.login(studentLoginDto);

        if (ObjectUtils.isNotEmpty(student)) {
            //下发Jwt
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("sid", student.getSid());
            claims.put("sname", student.getSname());

            // String token = Jwts.builder()
            //         .setClaims(claims)
            //         .signWith(SignatureAlgorithm.HS256, "token")
            //         .setExpiration(new Date(System.currentTimeMillis() + 60 * 1000))
            //         .compact();
            String token = JwtUtils.createJwt(claims);
            return R.success(token);
        } else {
            return R.error("NOT_LOGIN");
        }
        // return student == null ? R.error("登录失败") : R.success(student);
    }

    @PostMapping("/add")
    @ApiOperation("学生新增-mybatis")
    public String add(@RequestBody StudentDto studentDto) {
        stuService.add(studentDto);
        return "this ok";
    }

    @GetMapping("/list")
    @ApiOperation("查询全部-mybatis")
    public List<Student> list() {
        return stuService.list();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除-mybatis")
    public String delete(@PathVariable Long id) {
        stuService.delete(id);
        return "删除成功";
    }

    @PutMapping("/update")
    @ApiOperation("根据id修改-mybatis")
    public String update(@RequestBody Student student) {
        stuService.update(student);
        return "修改成功";
    }


    @GetMapping("/page")
    @ApiOperation("分页-mybatis")
    public StudentPageVO page(PageDto pageDto) {
        System.out.println(pageDto);
        return stuService.page(pageDto);
    }

    //会话技术
    //cookie
    @GetMapping("/setCookie")
    @ApiOperation("设置cookie")
    public R cookie(HttpServletResponse response) {
        response.addCookie(new Cookie("login_sname", "shanji"));
        return R.success();
    }

    @GetMapping("/getCookie")
    @ApiOperation("获取cookie")
    public R getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("login_sname".equals(cookie.getName())) {
                System.out.println("login_sname" + ":" + cookie.getValue());
            }
        }
        return R.success();
    }

    @GetMapping("/s1")
    @ApiOperation("设置session")
    public R setSession(HttpSession session) {
        session.setAttribute("login_sessionStu", "shanji");
        return R.success();
    }

    @GetMapping("/s2")
    @ApiOperation("获取session")
    public R getSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object login_sessionStu = session.getAttribute("login_sessionStu");
        return R.success(login_sessionStu);
    }

    //以下是用mybatisplus实现的增删改查
    // @PostMapping("/addplus")
    // // @ApiOperation("新增-mybatisplus")
    // public String addPlus(@RequestBody Student student) {
    //
    //     stuService.save(student);
    //     return "添加成功";
    // }
    //
    // @GetMapping("/listplus")
    // // @ApiOperation("查询全部-mybatisplus")
    // public List<Student> listPlus() {
    //     return stuService.list(null);
    // }
    //
    //
    // @DeleteMapping("/deleteplus/{id}")
    // // @ApiOperation("根据id删除-mybatisplus")
    // public String deletePlus(@PathVariable Long id) {
    //     boolean b = stuService.removeById(id);
    //     if (b) {
    //         return "删除成功";
    //     } else {
    //         return "数据不存在/删除失败";
    //     }
    // }
    //
    //
    // @PutMapping("/updateplus")
    // // @ApiOperation("根据id修改-mybatisplus")
    // public String updatePlus(@RequestBody Student student) {
    //
    //     boolean update = stuService.updateById(student);
    //     if (update) {
    //         return "修改成功";
    //     } else {
    //         return "修改失败";
    //     }
    // }
    //
    // @GetMapping("/pageplus")
    // // @ApiOperation("分页-mybatisplus")
    // public StudentPageVO pagePlus(PageDto pageDto) {
    //     Page<Student> objectPage = new Page<>(pageDto.getPage(), pageDto.getPageSize());
    //     // Page<Student> page = stuService.page(objectPage);
    //     // Page<Student> page = stuService.page(objectPage, null);
    //     Page<Student> studentPage = stuMapper.selectPage(objectPage, null
    //             // Wrappers.<Student>lambdaQuery().ge(Student::getSage, "2000-11-11 12:00:00")
    //     );
    //     return new StudentPageVO(studentPage.getTotal(), studentPage.getRecords());
    // }
}
